package com.me.study.jdk8.methodReferences;

import org.junit.Test;

/**
 * 通过对象名引用成员方法: <p>
 *      person::getAge <p></p>
 * 引用第一个参数实例的实例方法, 通过类名引用实例方法: <p>
 *      String::equals
 *
 * @author ME
 * @date 2020/9/17
 */
public class TestObjMethod {

    /**
     * 引用 实例方法: 某个实例方法结构上与 @FunctionalInterface中对应的方法
     * 相同（入参、返回相同, 无需关心 public、static、abstract 等修饰符）, 那么就可以直接引用给 @FunctionalInterface <p>
     * 见: {@link com.me.study.concurrent.thread.ThreadExecutionOrder#threadWait()}
     */
    @Test
    public void test3() {
        com.me.study.jdk8.methodReferences.MethodReference methodReference = new com.me.study.jdk8.methodReferences.MethodReference();
        MethodReference reference = (content, name) -> {
            System.out.println("Reference: " + content + ", " + name);
        };
        MethodReference reference2 = methodReference::getPrintStr;
        reference.sayHello("Goodbye", "Jack");
    }
    @FunctionalInterface
    public interface MethodReference {
        void sayHello(String content, String name);
    }

    /**
     * 引用 第一个参数实例的实例方法: <p>
     *     引用 str1.compareTo(str2)
     */
    @Test
    public void test4() {
        MyCompare compare1 = (str1, str2) -> str1.compareTo(str2);
        MyCompare compare = String::compareTo;
        // -1
        // System.out.println("a".compareTo("b"));
        System.out.println(compare.doMyCompare("a", "b"));

        // 自己建一个 参数实例的实例方法 引用
        System.out.println("\n自己建一个 参数实例的实例方法 引用");
        com.me.study.jdk8.methodReferences.MethodReference methodReference = new com.me.study.jdk8.methodReferences.MethodReference();
        MyCompare2 myCompare21 = new MyCompare2() {
            @Override
            public int doMyCompare(com.me.study.jdk8.methodReferences.MethodReference reference, String str1, String str2) {
                return reference.doMyCompare(str1, str2);
            }
        };
        MyCompare2 myCompare22 = (reference, str1, str2) -> {
            return reference.doMyCompare(str1, str2);
        };
        MyCompare2 myCompare2 = com.me.study.jdk8.methodReferences.MethodReference::doMyCompare;
        System.out.println(myCompare2.doMyCompare(methodReference, "a", "b"));
    }

    @FunctionalInterface
    public interface MyCompare {
        int doMyCompare(String str1, String str2);
    }

    @FunctionalInterface
    public interface MyCompare2 {
        int doMyCompare(com.me.study.jdk8.methodReferences.MethodReference reference, String str1, String str2);
    }

}
